#include "StdAfx.h"
#include "ExecOperation.h"

namespace HotKeyOperation
  {
  //------------------------------------------------------------------------

  ExecOperation::ExecOperation(std::wstring & i_exec_command, 
    bool i_wait_for_process /*= true*/, 
    unsigned int i_delay_before /*= 0*/, unsigned int i_delay_after /*= 0*/)
    :m_exec_command(i_exec_command)
    ,m_wait_for_process(i_wait_for_process)
    ,IOperation(i_delay_before, i_delay_after)
    {
    }

  //------------------------------------------------------------------------

  ExecOperation::ExecOperation(const wchar_t * i_exec_command, 
    bool i_wait_for_process /*= true*/, 
    unsigned int i_delay_before /*= 0*/, unsigned int i_delay_after /*= 0*/)
    :m_exec_command(i_exec_command)
    ,m_wait_for_process(i_wait_for_process)
    ,IOperation(i_delay_before, i_delay_after)
    {
    }

  //------------------------------------------------------------------------

  void ExecOperation::DoOperate()
    {
    STARTUPINFO startup_info;
    PROCESS_INFORMATION process_info;

    ZeroMemory(&startup_info, sizeof(startup_info));
    startup_info.cb = sizeof(startup_info);
    ZeroMemory(&process_info, sizeof(process_info));

    CreateProcess(
    /*LPCWSTR lpApplicationName = */                    0,
    /*LPWSTR lpCommandLine = */                         const_cast<wchar_t*>(m_exec_command.c_str()),
    /*LPSECURITY_ATTRIBUTES lpProcessAttributes = */    0,
    /*LPSECURITY_ATTRIBUTES lpThreadAttributes = */     0,
    /*BOOL bInheritHandles = */                         FALSE,
    /*DWORD dwCreationFlags = */                        0,
    /*LPVOID lpEnvironment = */                         0,
    /*LPCWSTR lpCurrentDirectory = */                   0,
    /*LPSTARTUPINFOW lpStartupInfo = */                 &startup_info,
    /*LPPROCESS_INFORMATION lpProcessInformation = */   &process_info      
      );

    if (m_wait_for_process)
      WaitForSingleObject(process_info.hProcess, INFINITE);
    }

  //------------------------------------------------------------------------

  }
